#include "regist.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFormLayout>
#include <QHostAddress>

Regist::Regist(QWidget *parent) : QDialog(parent)
{


    tcpsocket.connectToHost(QHostAddress("127.0.0.1"),8888);


    setWindowTitle("Register");
    resize(300, 300);

    // Create widgets
    usernameLabel = new QLabel("用户名:");
    usernameLineEdit = new QLineEdit;
    passwordLabel = new QLabel("密码:");
    passwordLineEdit = new QLineEdit;
    passwordLineEdit->setEchoMode(QLineEdit::Password);
    confirmPasswordLabel = new QLabel("再次输入密码:");
    confirmPasswordLineEdit = new QLineEdit;
    confirmPasswordLineEdit->setEchoMode(QLineEdit::Password);
//    emailLabel = new QLabel("Email:");
//    emailLineEdit = new QLineEdit;
    registButton = new QPushButton("Regist");
    statusLabel = new QLabel;
    statusLabel->setStyleSheet("color: red");

    // Create layouts
    QVBoxLayout *mainLayout = new QVBoxLayout;
    QHBoxLayout *buttonLayout = new QHBoxLayout;
    QFormLayout *formLayout = new QFormLayout;

    // Add widgets to layouts
    formLayout->addRow(usernameLabel, usernameLineEdit);
    formLayout->addRow(passwordLabel, passwordLineEdit);
    formLayout->addRow(confirmPasswordLabel, confirmPasswordLineEdit);
//    formLayout->addRow(emailLabel, emailLineEdit);
    buttonLayout->addWidget(registButton);
    buttonLayout->addStretch();
    buttonLayout->addWidget(statusLabel);
    mainLayout->addLayout(formLayout);
    mainLayout->addLayout(buttonLayout);

    // Set layout
    setLayout(mainLayout);

    // Connect signals and slots
    connect(registButton, &QPushButton::clicked, this, &Regist::on_registButton_clicked);
    connect(&tcpsocket,SIGNAL(readyRead()),this,SLOT(recvMsg()));
}

Regist::~Regist()
{
}

Regist &Regist::getInstance()
{
    static Regist instance;
    return instance;
}

void Regist::on_registButton_clicked()
{
    QString username = usernameLineEdit->text();
    QString password = passwordLineEdit->text();
    QString confirmPassword = confirmPasswordLineEdit->text();
    //QString email = emailLineEdit->text();


    if(username.isEmpty() || password.isEmpty() || confirmPassword.isEmpty()) {
        statusLabel->setText("请填写完整信息！");
        return;
    }

    if (password != confirmPassword) {
        statusLabel->setText("两次密码不一致！");
        return;
    }

    //qDebug() << username << password;
    PBS *pbs = mkPBS(0);
    pbs->MsgType = REGIST_REQUEST;
//    strncpy(pbs->caData,username.toStdString().c_str(),32);
//    strncpy(pbs->caData+32,password.toStdString().c_str(),32);
    tcpsocket.write((char*)pbs,pbs->PBSLen);
    //qDebug() << pbs->caData << "111111111111";
    free(pbs);
    pbs = NULL;
}


void Regist::recvMsg()
{
    uint PBSLen =0;
    tcpsocket.read((char*)&PBSLen,sizeof(uint));
    uint MsgLen =PBSLen-sizeof (PBS);
    PBS *pbs = mkPBS(MsgLen);
    tcpsocket.read((char*)pbs+sizeof(uint),PBSLen-sizeof (uint));
    qDebug() << pbs->MsgType << (char*)pbs->Msg;

    switch (pbs->MsgType) {
    case REGIST_RESPOND:
    {
//        if(0 == strcmp(pbs->caData,REGIST_OK)){
//            QMessageBox::information(this,"注册","注册成功");
//            Regist::close();
//        }
//        if(0 == strcmp(pbs->caData,REGIST_FAILED)){
//            QMessageBox::warning(this,"注册","注册失败:用户名重复");
//        }
        break;
    }
    default:
        break;
    }

    free(pbs);
    pbs = NULL;
}


